/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import pojo.Danhmuc;
import until.MySqlDataAccessHelper;

/**
 *
 * @author Administrator
 */
public class DanhMuc_DAO {
     public static ArrayList<Danhmuc> layDanhSachDanhMuc(){
        ArrayList<Danhmuc>ds = new ArrayList<Danhmuc>();
        String sql=String.format("Select *from danhmuc");
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {                
                Danhmuc danhmuc= new Danhmuc();
                danhmuc.setId(rs.getInt("ID"));
                danhmuc.setNgayTao(rs.getDate("NgayTao"));
                danhmuc.setTenDanhMuc(rs.getString("TenDanhMuc"));
                ds.add(danhmuc);
            }
        } catch (SQLException e) {
            System.out.print(e.getMessage());
        }
        helper.close();
        return ds;
    }
    
    public static int LaySoLuongSanPham(int maDanhMuc){
        String sql=String.format("select count(*) as SoLuong from Deal where MaDanhMuc="+maDanhMuc+"");
        int soLuong=-1;
        try {
            MySqlDataAccessHelper helper= new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            if (rs.next()) {
                soLuong= rs.getInt("SoLuong");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return  soLuong;
    }
    
//    public static int tinhSoLuongTrang(int maDanhMuc){
//        int soTrang;
//        int soSanPhamTrenTrang=ThamSo.laySoLuongSanPhamTrenTrang();
//        int soLuong=DanhMucDao.LaySoLuongSanPham(maDanhMuc);
//        soTrang=soLuong/soSanPhamTrenTrang;
//        if(soLuong%soSanPhamTrenTrang!=0){
//            soTrang++;
//        }
//        return soTrang;
//    }
//    
}
